package com.hxkj.service.wechat.impl;

import com.hxkj.client.service.AppletClassifyServiceClient;
import com.hxkj.domain.enums.AppletClassifyExceptionEnum;
import com.hxkj.domain.po.AppletClassify;
import com.hxkj.domain.po.AppletClassifyTree;
import com.hxkj.service.wechat.AppletClassifyService;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AppletClassifyServiceImpl
 * @Description TODO
 * @Author Mr.zheng
 * @Date 2018/6/11 15:21
 * @Version 1.0
 */
@Service
@Slf4j
public class AppletClassifyServiceImpl implements AppletClassifyService{

    private static final Logger LOGGER = LoggerFactory.getLogger(AppletClassifyServiceImpl.class);

    @Autowired
    private AppletClassifyServiceClient appletClassifyServiceClient;

    @Override
    public void saveClassify(AppletClassify appletClassify) throws Exception{
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            if (appletClassify==null){
                throw new CustomException(AppletClassifyExceptionEnum.CLASSIFY_NOT_NAME);
            }
            if (null==appletClassify.getClassifyName()|| appletClassify.getClassifyName()==""){
                throw new CustomException(AppletClassifyExceptionEnum.CLASSIFY_NOT_NAME);
            }
            if (appletClassify.getSiteId()==null){
                throw new CustomException(AppletClassifyExceptionEnum.CLASSIFY_NOT_SITEID);
            }
            resultMessageStruct = appletClassifyServiceClient.saveClassify(appletClassify);
            if(resultMessageStruct.getCode()!=200){
                throw new CustomException(AppletClassifyExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public ResultPageStruct<AppletClassify> getClassifyList(AppletClassify appletClassify, PageTo pageParam) throws Exception{
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<AppletClassify> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(appletClassify);
        parameterDataStruct.setPageTo(pageParam);
        ResultPageStruct<AppletClassify> resultPageStruct = new ResultPageStruct<>();
        try {
            resultPageStruct = appletClassifyServiceClient.getClassifyList(parameterDataStruct);
            resultPageStruct.setCode(null);
            resultPageStruct.setMessage(null);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("获取文章列表失败");
        }
        return resultPageStruct;
    }

    @Override
    public List<AppletClassifyTree> getClassifyTreeList(AppletClassify appletClassify) throws Exception{
        List<AppletClassifyTree> appletClassifyTreeList = new ArrayList<AppletClassifyTree>();
        try {
            if(appletClassify.getKeyword().equals("") || StringUtils.isEmpty(appletClassify.getKeyword())) {
                appletClassifyTreeList = appletClassifyServiceClient.getClassifyTreeList(appletClassify);
            }else {
                appletClassifyTreeList = appletClassifyServiceClient.getClassifyTreeListByKeyword(appletClassify);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("获取文章分类失败");
        }
        return appletClassifyTreeList;
    }

    @Override
    public void modifyClassify(AppletClassify appletClassify) throws Exception{
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            if (appletClassify==null){
                throw new CustomException(AppletClassifyExceptionEnum.CLASSIFY_NOT_VOID);
            }
            if (appletClassify.getClassifyId()==null){
                throw new CustomException(AppletClassifyExceptionEnum.CLASSIFY_NOT_VOID);
            }
            if (appletClassify.getSiteId()==null){
                throw new CustomException(AppletClassifyExceptionEnum.CLASSIFY_NOT_SITEID);
            }
            resultMessageStruct = appletClassifyServiceClient.modifyClassify(appletClassify);
            if(resultMessageStruct.getCode()!=200){
                throw new CustomException(AppletClassifyExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void removeClassify(AppletClassify appletClassify) throws Exception{
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            if (appletClassify==null){
                throw new CustomException(AppletClassifyExceptionEnum.CLASSIFY_NOT_VOID);
            }
            if (appletClassify.getClassifyIds().equals("")){
                throw new CustomException(AppletClassifyExceptionEnum.CLASSIFY_NOT_VOID);
            }
            resultMessageStruct = appletClassifyServiceClient.removeClassify(appletClassify);
            if(resultMessageStruct.getCode()!=200){
                throw new CustomException(AppletClassifyExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void modifySortNum(Map<String, Object> map) throws Exception{
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            if (map.get("siteId")==null){
                throw new CustomException(AppletClassifyExceptionEnum.CLASSIFY_NOT_SITEID);
            }
            resultMessageStruct = appletClassifyServiceClient.modifySortNum(map);
            if(resultMessageStruct.getCode()!=200){
                throw new CustomException(AppletClassifyExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public List<AppletClassifyTree> getShowClassifyTreeList(AppletClassify appletClassify) throws Exception{

        List<AppletClassifyTree> appletClassifyTreeList = new ArrayList<AppletClassifyTree>();
        try {
            appletClassifyTreeList =  appletClassifyServiceClient.getShowClassifyTreeList(appletClassify);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info("获取文章分类失败");
        }
        return appletClassifyTreeList;
    }
}
